// Do not remove the include below
#include "SerialRFEmitter.h"
#include <VirtualWire.h>
#include "HardwareSerial.h"
#include <Streaming.h>

int serIn; // var that will hold the bytes-in read from the serialB
char txbuf[50]; // array that will hold the different bytes 100=100characte// -> you must state how long the array will be else it won't work.
char serialbuffer[50];
int serInIndx = 0; // index of serInString[] in which to insert the next incoming byte

extern HardwareSerial Serial;
//The setup function is called once at startup of the sketch
void setup() {
	Serial.begin(9600);
	Serial << "setup" << endl;
	vw_setup(4000); // Bits per sec
	vw_set_tx_pin(2);
	//memset(txbuf, '\0', sizeof(txbuf));

}

String readSerialString() {
	int sb;
	serInIndx = 0;
	if (Serial.available()) {
		//Serial.print("reading Serial String: "); //optional confirmation
		while (Serial.available()) {
			sb = Serial.read();
			serialbuffer[serInIndx] = sb;
			serInIndx++;
			//serialWrite(sb); //optional confirmation
		}
		serialbuffer[serInIndx] = '\0';
		String result = String(serialbuffer);
		return result;
	} else {
		return NULL;
	}
}
// The loop function is called in an endless loop
void loop() {
	String s = readSerialString();
//	String s = String("turnOn:2");
	if (s!=NULL) {
//		memset(txbuf, '\0', sizeof(txbuf));
		for (int i=0;i<strlen(txbuf);i++) {
			txbuf[i]='\0';
		}
		s.getBytes((unsigned char *)txbuf,50,0);
		Serial << txbuf<< endl;
		vw_send((uint8_t *) txbuf, strlen(txbuf));
		vw_wait_tx();

	}
		delay(500);
}


